﻿// Decompiled with JetBrains decompiler
// Type: MCM.LightInject.TypeHelper
// Assembly: MCMv5, Version=5.10.2.0, Culture=neutral, PublicKeyToken=null
// MVID: 7AB326B9-B2B4-41C2-9177-68E944071101
// Assembly location: D:\steam\steamapps\workshop\content\261550\2859238197\bin\Win64_Shipping_Client\MCMv5.dll

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;

#nullable disable
namespace MCM.LightInject
{
  [ExcludeFromCodeCoverage]
  internal static class TypeHelper
  {
    public static Type TryMakeGenericType(Type openGenericType, Type[] closedGenericArguments)
    {
      try
      {
        return openGenericType.MakeGenericType(closedGenericArguments);
      }
      catch (Exception ex)
      {
        return (Type) null;
      }
    }

    public static bool IsEnumerableOfT(this Type type)
    {
      TypeInfo typeInfo = type.GetTypeInfo();
      return typeInfo.IsGenericType && typeInfo.GetGenericTypeDefinition() == typeof (IEnumerable<>);
    }

    public static bool IsListOfT(this Type type)
    {
      TypeInfo typeInfo = type.GetTypeInfo();
      return typeInfo.IsGenericType && typeInfo.GetGenericTypeDefinition() == typeof (IList<>);
    }

    public static bool IsCollectionOfT(this Type type)
    {
      TypeInfo typeInfo = type.GetTypeInfo();
      return typeInfo.IsGenericType && typeInfo.GetGenericTypeDefinition() == typeof (ICollection<>);
    }

    public static bool IsReadOnlyCollectionOfT(this Type type)
    {
      TypeInfo typeInfo = type.GetTypeInfo();
      return typeInfo.IsGenericType && typeInfo.GetGenericTypeDefinition() == typeof (IReadOnlyCollection<>);
    }

    public static bool IsReadOnlyListOfT(this Type type)
    {
      TypeInfo typeInfo = type.GetTypeInfo();
      return typeInfo.IsGenericType && typeInfo.GetGenericTypeDefinition() == typeof (IReadOnlyList<>);
    }

    public static bool IsLazy(this Type type)
    {
      TypeInfo typeInfo = type.GetTypeInfo();
      return typeInfo.IsGenericType && typeInfo.GetGenericTypeDefinition() == typeof (Lazy<>);
    }

    public static bool IsFuncRepresentingService(this Type type)
    {
      TypeInfo typeInfo = type.GetTypeInfo();
      return typeInfo.IsGenericType && typeInfo.GetGenericTypeDefinition() == typeof (Func<>);
    }

    public static bool IsFuncWithParameters(this Type type)
    {
      TypeInfo typeInfo = type.GetTypeInfo();
      if (!typeInfo.IsGenericType || type.IsFuncRepresentingNamedService())
        return false;
      Type genericTypeDefinition = typeInfo.GetGenericTypeDefinition();
      return genericTypeDefinition == typeof (Func<,>) || genericTypeDefinition == typeof (Func<,,>) || genericTypeDefinition == typeof (Func<,,,>) || genericTypeDefinition == typeof (Func<,,,,>);
    }

    public static bool IsFuncRepresentingNamedService(this Type type)
    {
      TypeInfo typeInfo = type.GetTypeInfo();
      return typeInfo.IsGenericType && typeInfo.GetGenericTypeDefinition() == typeof (Func<,>) && typeInfo.GenericTypeArguments[0] == typeof (string);
    }

    public static bool IsClosedGeneric(this Type type)
    {
      TypeInfo typeInfo = type.GetTypeInfo();
      return typeInfo.IsGenericType && !typeInfo.IsGenericTypeDefinition;
    }

    public static Type GetElementType(Type type)
    {
      TypeInfo typeInfo = type.GetTypeInfo();
      Type[] genericTypeArguments = typeInfo.GenericTypeArguments;
      return typeInfo.IsGenericType && genericTypeArguments.Length == 1 ? genericTypeArguments[0] : type.GetElementType();
    }

    public static object GetDefaultValue(Type type)
    {
      return typeof (TypeHelper).GetTypeInfo().GetDeclaredMethod("GetDefaultValueInternal").MakeGenericMethod(type).Invoke((object) null, new object[0]);
    }

    private static T GetDefaultValueInternal<T>() => default (T);
  }
}
